package com.hilinos.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hilinos.base.api.vo.OrgInfoVo;
import com.hilinos.base.user.entity.pojo.OrgInfoPoJo;
import com.hilinos.base.mapper.IOrgInfoMapper;
import com.hilinos.base.service.OrgInfoService;
import com.hilinos.common.common.ReturnCode;
import com.hilinos.common.util.AssertUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hilinos
 * @since 2022-04-18
 */
@Service
@Slf4j
public class OrgInfoServiceImpl extends ServiceImpl<IOrgInfoMapper, OrgInfoPoJo> implements OrgInfoService {

    @Override
    public OrgInfoPoJo detail(String orgId, String appid) {
        QueryWrapper<OrgInfoPoJo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(OrgInfoPoJo::getAppid, appid)
                .eq(OrgInfoPoJo::getOrgId, orgId);
        return getOne(queryWrapper);
    }

    @Override
    public OrgInfoPoJo parent(String orgId, String appid) {
        OrgInfoPoJo orgInfo = detail(orgId, appid);
        AssertUtils.isNotEmpty(orgInfo, ReturnCode.DATA_NOT_EXIST.respMsg("机构不存在"));

        String pOrgId = orgInfo.getPOrgId();
        OrgInfoPoJo pOrg = detail(pOrgId, appid);
        AssertUtils.isNotEmpty(pOrg, ReturnCode.DATA_NOT_EXIST.respMsg("父机构不存在"));
        return pOrg;
    }

    @Override
    public List<OrgInfoPoJo> child(String orgId, String appid) {
        QueryWrapper<OrgInfoPoJo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(OrgInfoPoJo::getAppid, appid)
                .eq(OrgInfoPoJo::getPOrgId, orgId);
        return list(queryWrapper);
    }

    @Override
    public List<OrgInfoVo> childs(String orgId, String appid) {
        QueryWrapper<OrgInfoPoJo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(OrgInfoPoJo::getAppid, appid)
                .apply(!ObjectUtils.isEmpty(orgId), "find_in_set('" + orgId + "',p_org_nodes)")
                .orderByDesc(OrgInfoPoJo::getOrgId);
        List<OrgInfoVo> orgInfoVos = OrgInfoVo.pojos2Vos(list(queryWrapper));

        Map<String, OrgInfoVo> map = new HashMap<>();

        List<OrgInfoVo> results = new ArrayList<>();
        for (OrgInfoVo orgInfoVo : orgInfoVos) {
            orgInfoVo.setChilds(new ArrayList<>());
            if (ObjectUtils.isEmpty(orgId)) {
                if (ObjectUtils.isEmpty(orgInfoVo.getPOrgId())) {
                    results.add(orgInfoVo);
                }
            } else if (orgId.equals(orgInfoVo.getPOrgId())) {
                results.add(orgInfoVo);
            }
            map.put(orgInfoVo.getOrgId(), orgInfoVo);
        }

        map.forEach((orgId1, orgInfoVo) -> {
            if (map.containsKey(orgInfoVo.getPOrgId())) {
                map.get(orgInfoVo.getPOrgId()).getChilds().add(orgInfoVo);
            }
        });
        return results;
    }

    @Override
    public void edit(OrgInfoPoJo orgInfo) {
        QueryWrapper<OrgInfoPoJo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(OrgInfoPoJo::getAppid, orgInfo.getAppid())
                .eq(OrgInfoPoJo::getOrgId, orgInfo.getOrgId());
        update(orgInfo, queryWrapper);
    }
}